<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * CodeIgniter Template Class        
 *
 * @package         CodeIgniter
 * @subpackage      Libraries
 * @category        Libraries
 * @author          Dac Diep Vuong   
 */
define ('JQUERY_OPEN', "\njQuery(document).ready(function () {\n");
define ('JQUERY_CLOSE', "\n});\n");
class CI_Template
{
    var $CI;
    var $config;
    
    public $data = array();  
    
    /**
     * Template layout
     */
    var $layout = 'default'; 
    
    private $_stylesheet = array();
    private $_cssText = '';
        
    private $_javascript = array();
    private $_jsText = array(); 
    
    function CI_Template() {
        $this->CI =& get_instance(); 
        include(APPPATH.'config/client'.EXT);
        $this->config = $config;
    }
    
    public function setLayout($layout = false) {
        if($layout) {
            $this->layout = $layout;
        } 
        // Load comperssor for lib
        $this->CI->load->library('compressor'); 
        // Css
        if(sizeof($this->_stylesheet) > 0) {
            $this->data['css_for_layout'] = $this->css();
        }
        // Js TOP
        if(isset($this->_javascript['TOP'])) {
            $this->data['jstop_for_layout'] = $this->js('TOP');
        }
        // Js BUTTON
        if(isset($this->_javascript['BOTTOM'])) {
            $this->data['jsbuttom_for_layout'] = $this->js('BOTTOM');
        }
        $this->CI->load->view('layout/'. $this->layout, $this->data);
    }
    
    /**
     * Add javascript file
     * 
     * @param string    $file
     * @param string    $position the position of js file, support TOP|BOTTOM 
     * @param array     $option
     */
    public function addJs($file, $position = 'BOTTOM', $option = array()) {
        $position = strtoupper($position);
        $this->_javascript[$position][$file] = $option;
    }
    
    /**
     * add Js code
     * @param string    $code js code
     * @param string    $position TOP|BOTTOm
     * @param string    $lib, js framework like 'jQuery', 'MooTool', 'Dojo' etc
     */
    public function addJsCode($code, $position = 'BOTTOM', $lib = 'jQuery') {
        $position = strtoupper($position);
        $this->_jsText[$position][$lib][] = $code;
    }
    
    /**
     * render js link
     * @param string    $pos
     */
    private function js($pos = 'BOTTOM') {
        $pos = strtoupper($pos);
        $jsCode = '';
        if (isset($this->_jsText[$pos])) {
            foreach ($this->_jsText[$pos] as $lib=>$code) {
                if ('standard' == $lib) {
                    $jsCode .= implode("\n", $code) ."\n";                
                } else {
                    $open = constant(strtoupper($lib) .'_OPEN');
                    $close = constant(strtoupper($lib) .'_CLOSE');
                    $jsCode .= $open .implode("\n", $code) .$close;
                }                
            }
            $jsCode = "\n<script type=\"text/javascript\">" .$jsCode ."</script>\n";  
        }        
        $js = '';
        if (isset($this->_javascript[$pos])) {
            if ($this->config['compile_js']) {    
                $js = $this->CI->compressor->js($this->_javascript[$pos]);
                $js = '<script type="text/javascript" src="'. base_url() .'temp/' .$js .'?v='. $this->config['version_client'] .'"></script>';
            } else {
                foreach($this->_javascript[$pos] as $file => $option) {
                    $js .= '<script type="text/javascript" src="'. base_url() .'js/'. $file .'?v='. $this->config['version_client'] .'"></script>';
                }                
            }            
        }
        
        if ('TOP' == $pos) {
            $js .= $jsCode;            
        } else {
            $js = $jsCode .$js;
        }
        
        return $js ."\n";
    }  
        
    /**
     * Add Stylesheet
     *
     * @param string $file. Link to file
     * @param array $media. Stylesheet Media style
     */
    public function addCss($file, $media = array()) {
        $this->_stylesheet[$file] = $media;
    }
    
    private function css() {
        if (count($this->_stylesheet) === 0) {
            return null;
        }
        
        if ($this->config['compile_css']) {   
            $css = $this->CI->compressor->css($this->_stylesheet);
            $css = '<link rel="stylesheet" type="text/css" href="'. base_url() .'temp/' .$css .'?v='. $this->config['version_client'] .'" media="screen" />';
            return $css;    
        } else {
            $css = '';
            foreach ($this->_stylesheet as $file => $media) {
                $media = ((is_array($media))? implode(', ', $media) : 'screen') .'"';
                if (null != $media) {
                    $media = 'screen';                
                }
                $css .= '<link rel="stylesheet" type="text/css" href="' . base_url() .'css/'. $file .'?v='. $this->config['version_client'] .'"' .' media="' .$media .'" />';                        
            }
            
            return $css . "\n";
        }
    }
}  
?>
